import { Component } from 'react';

import { Group,Button,Toast,TextareaField} from 'saltui';
import DB from '../../../app/db';

const {Count} = TextareaField;
export default class PageHome extends Component {

    constructor(props){
        super(props);
        this.state = {            
            backReason:"",
        }
    }

    componentWillMount(){
        dd.biz.navigation.setTitle({
            title: '回退原因',
            onSuccess: function(result) {},
            onFail: function() {}
        });

        let t = this;
        if(t.props.params.type == "view"){
            DB.QuesModuleAPI.getBackReason({id:t.props.params.id}).then((content) => {
                if(content.type == 'success'){
                    t.setState({
                        backReason:content.message
                    })
                }else{
                    Toast.show({
                        type: 'error',
                        content: result.message,
                    });
                }
            })
        }
    }

    handleChange(value) {
        this.setState({ backReason:value });
    }

    restart(){
        let t = this;
        let id = this.props.params.quesId;
        DB.QuesModuleAPI.entityIsExist({id:id}).then((content) => {
            if(content == 'exist'){
                location.hash='questionnaire/ques_info/'+id+"/0";
            }else{
                Toast.show({
                    type: 'error',
                    content: '当前数据已删除',
                });
                history.back();
            }
        }).catch((error) => {
        }); 
    }

    submit(){
        let t = this;
        let backReason = t.state.backReason;
        if(backReason.trim() ==''){
            Toast.show({
                type: 'error',
                content: '回退原因不能为空'
            });
            return;
        }else if(backReason.length > 200){
            Toast.show({
                type: 'error',
                content: '回退原因不能超过200个字符',
            });
            return;
        }else if(isEmojiCharacter(backReason)){
            Toast.show({
                type: 'error',
                content: '回退原因不能输入表情',
            });
            return;
        }
        DB.QuesModuleAPI.quesBack({ id:t.props.params.id, backReason:backReason }).then((result)=>{
            if(result.type == 'success'){
                Toast.show({
                    type: 'success',
                    content: result.message,
                });
                history.back();
            }else{
                Toast.show({
                    type: 'error',
                    content: result.message,
                });
            }
        })
    }

    render(){
        return (
            <Group.List>
                <TextareaField
                    required
                    label="回退原因"
                    minRows={6}
                    maxRows={21}
                    readOnly={this.props.params.type == "view" ? true : false}
                    placeholder="1-200个字符"
                    value={this.state.backReason}
                    onChange={(value) => {
                        if(value.length <= 200){
                            this.handleChange(value);
                        }
                    }}>
                    <Count total={200} length={this.state.backReason.length}/>
                </TextareaField>
                <div className={this.props.params.type == "edit" ? "dd-bottom-fixed" : "t-DN"}><Button type="primary" onClick={this.submit.bind(this)}>提交</Button></div>
                <div className={this.props.params.type == "view" ? "dd-bottom-fixed" : "t-DN"}><Button type="primary" onClick={this.restart.bind(this)}>重新填写</Button></div>
            </Group.List>
        )
    }
}